#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
#include "sys/types.h"
#include "fcntl.h"
#include "poll.h"
#include "sys/select.h"
#include "sys/time.h"
#include "signal.h"
#include "linux/ioctl.h"

#define KEY0VALUE  0xF0
#define INVAVALUE  0x00

static int fd = 0;

static void sigio_signal_func(int signum)
{
    int err = 0;

    unsigned int keyvalue = 0;

    err = read(fd,&keyvalue,sizeof(keyvalue));
    if (err < 0)
    {
        /* code */
    }
    else
    {
        printf("sigio signal keyvalue:%d\r\n",keyvalue);
    }
}

int main(int argc, char *argv[])
{

    int flags;
    unsigned char *filename;

    if (argc != 3)
    {
        printf("Usage: ERROR\r\n");
        return -1;
    }
    
    filename = argv[1];

    fd = open(filename,O_RDWR);        //非阻塞访问
    if (fd < 0)
    {
        printf("file open failed\r\n");
        return -1;
    }

    signal(SIGIO,sigio_signal_func);//设置信号对应的信号处理函数

    fcntl(fd,F_SETOWN,getpid());    //通知内核当前应用程序的进程号
    flags = fcntl(fd,F_GETFD);      //获取当前进程状态
    fcntl(fd,F_SETFL,flags | FASYNC);//开启异步通知功能，驱动程序的fasync函数执行-》初始化异步通知结构体

    while (1)
    {
        sleep(2);
    }
    
    close(fd);
    
    return 0;
}